﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using Microsoft.VisualBasic;
using WebChart;
using Votations.NSurvey;
using Votations.NSurvey.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Enums;
using Votations.NSurvey.Resources;
using Votations.NSurvey.Web;
using Votations.NSurvey.BusinessRules;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Summary description for PieChart.
    /// </summary>
    public partial class BarChartReport : PageBase
    {
        public override string ActionCode
        {
            get
            {
                return "103";//报告
            }
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {

            _questionId =
                Information.IsNumeric(Request["QuestionId"]) ? int.Parse(Request["QuestionId"]) : -1;
            _filterId =
                Information.IsNumeric(Request["FilterId"]) ? int.Parse(Request["FilterId"]) : -1;
            _sortOrder =
                Request["SortOrder"] == null ? "ans" : Request["SortOrder"];

            if (_questionId == -1)
            {
                Response.End();
            }
            else if (!NSurveyUser.Identity.IsAdmin &&
                !NSurveyUser.Identity.HasAllSurveyAccess)
            {
                if (!new Question().CheckQuestionUser(_questionId, NSurveyUser.Identity.UserId))
                {
                    Response.End();
                }
            }

            ChartEngine engine = new ChartEngine();
            ChartCollection charts = new ChartCollection(engine);

            engine.Size = new Size(750, 400);
            engine.Charts = charts;

            WebChart.ColumnChart chart = new WebChart.ColumnChart();
            chart.Fill.Color = Color.FromArgb(121, 190, 250);
            chart.MaxColumnWidth = 80;
            chart.DataLabels.Visible = true;
            chart.DataLabels.Position = DataLabelPosition.Bottom;
            chart.Shadow.Visible = true;
            chart.DataLabels.NumberFormat = "0.00";

            SetQuestionData(engine, chart);
            SetMoreProperties(engine);

            charts.Add(chart);


            // send chart to browser
            Bitmap bmp;
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            bmp = engine.GetBitmap();
            bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
            memStream.WriteTo(Response.OutputStream);
            Response.End();

        }

        private void SetupSecurity()
        {
            if (NSurveyUser.Identity.IsAdmin ||
                NSurveyUser.HasRight(NSurveyRights.AccessReports))
            {
                return;
            }
            else
            {
                Response.End();
            }
        }

        private void SetMoreProperties(ChartEngine engine)
        {
            engine.YCustomEnd = 100;
            engine.ShowYValues = true;
            engine.ShowXValues = true;
            engine.ChartPadding = 50;
            engine.GridLines = WebChart.GridLines.Horizontal;
            engine.PlotBackground.Color = Color.FromArgb(240, 244, 248);
            engine.Background.Type = InteriorType.LinearGradient;
            engine.Background.EndPoint = new Point(750, 400);
            engine.Background.ForeColor = Color.FromArgb(245, 249, 251);
            engine.Background.Color = Color.FromArgb(177, 208, 214);
        }

        protected void SetQuestionData(ChartEngine engine, ColumnChart columnChart)
        {
            try
            {
                _dataSource = new Questions().GetQuestionResults(_questionId, _filterId, _sortOrder);
            }
            catch (QuestionNotFoundException)
            {
                return;
            }

            QuestionResultsData.AnswersRow[] answers = _dataSource.Questions[0].GetAnswersRows();

            // Set-up question text
            engine.Title = new ChartText();
            engine.Title.ForeColor = Color.FromArgb(0, 102, 118);
            engine.Title.Font = new Font("Tahoma", 12, FontStyle.Bold);
            engine.Title.Text = Server.HtmlDecode(System.Text.RegularExpressions.Regex.Replace(_dataSource.Questions[0].QuestionText, "<[^>]*>", " "));
            SetAnswerData(answers, GetVotersTotal(answers), engine, columnChart);
        }

        private float GetVotersTotal(QuestionResultsData.AnswersRow[] answers)
        {
            float total = 0;
            foreach (QuestionResultsData.AnswersRow answer in answers)
            {
                // Increment only selection answers that can be selected
                if ((((AnswerTypeMode)answer.TypeMode & AnswerTypeMode.Selection) > 0))
                {
                    total += answer.VoterCount;
                }
            }
            return total;
        }

        private void SetAnswerData(QuestionResultsData.AnswersRow[] answers, float totalOfVotes, ChartEngine engine, WebChart.ColumnChart columnChart)
        {
            float currentRate = 0;
            float totalRate = 0;

            foreach (QuestionResultsData.AnswersRow answer in answers)
            {
                if ((((AnswerTypeMode)answer.TypeMode & AnswerTypeMode.Selection) > 0))
                {
                    float VotePercent = 0;

                    if (totalOfVotes != 0)
                    {
                        if (answer.VoterCount == 0)
                            VotePercent = 0;
                        else
                            VotePercent = ((float)answer.VoterCount / (float)totalOfVotes) * 100;
                    }

                    // Add answer text & vote count
                    answer.AnswerText = Server.HtmlDecode(System.Text.RegularExpressions.Regex.Replace(answer.AnswerText, "<[^>]*>", " "));
                    string answerChartText;
                    answerChartText = string.Format("{0} ({1})",
                            answer.AnswerText, answer.VoterCount.ToString());
                    columnChart.Data.Add(new ChartPoint(answerChartText, VotePercent));

                    // Do we include this answer in the
                    // rating total
                    if (answer.RatePart)
                    {
                        currentRate++;
                        totalRate += currentRate * answer.VoterCount;
                    }
                }
            }

            StringFormat horizontalFormat = new StringFormat();
            horizontalFormat.LineAlignment = StringAlignment.Far;
            engine.XTitle = new ChartText();
            engine.XTitle.StringFormat = horizontalFormat;
            engine.XTitle.Font = new System.Drawing.Font("Verdana", 8);

            // Do we show the average rating 
            if (_dataSource.Questions[0].RatingEnabled)
            {
                double meanRate = 0;

                if (totalOfVotes == 0)
                    meanRate = 0;
                else
                    meanRate = totalRate / totalOfVotes;

                engine.XTitle.Text = string.Format("{0}{1}{2}",
                    String.Format(ResourceManagerSingleton.Instance.GetString("TotalOfVotes"), totalOfVotes),
                    Environment.NewLine,
                    string.Format(ResourceManagerSingleton.Instance.GetString("RatingResults"), meanRate.ToString("##.##"), currentRate));
            }
            else
            {
                engine.XTitle.Text = String.Format(ResourceManagerSingleton.Instance.GetString("TotalOfVotes"), totalOfVotes);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion

        QuestionResultsData _dataSource = new QuestionResultsData();
        int _questionId,
            _filterId;
        string _sortOrder;

    }

}
